﻿using BW.EatRabbit.API.Application.Commands.SlaughterCompany;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers.SlaughterCompany
{
    public class CompanyCreateCommandHandler : IRequestHandler<CompanyCreateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Slaughter_Company> _companyRepository;
        public CompanyCreateCommandHandler(IBaseRepository<Slaughter_Company> companyRepository)
        {
            _companyRepository = companyRepository;

        }
        public Task<APIResult<int>> Handle(CompanyCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            Slaughter_Company company = new Slaughter_Company();
            company.CompanyName = request.CompanyName;
            company.ProvinceId = request.ProvinceId;
            company.CityId = request.CityId;
            company.CompanyAddress = request.CompanyAddress;
            company.FoundTime = request.FoundTime;
            company.CompanyProfile = request.CompanyProfile;
            company.ProductionBatch = request.ProductionBatch;
            company.Creator = request.UserNickname;

            _companyRepository.AddEntity(company);
            result.Code = ResultCode.添加成功;
            result.Message = ResultCode.添加成功.ToString();
            return Task.FromResult(result);
        }
    }
}
